//
//  YBAssetsDetailTabbar.m
//  Nectar
//
//  Created by famer on 2023/11/11.
//  Copyright © 2023 asance. All rights reserved.
//

#import "YBAssetsDetailTabbar.h"

@implementation YBAssetsDetailTabbar

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self addSubview:self.allButton];
        [self addSubview:self.outButton];
        [self addSubview:self.inButton];
        [self addSubview:self.failedButton];
        
        [self addSubview:self.sliderView];
        
        [self onSelectTabAction:self.allButton];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat width = self.frame.size.width;
    CGFloat leftMargin = 12;
    CGFloat topMargin = 16;
    CGFloat buttonWidth = width*0.25;
    CGFloat buttonHeight = 50;
    
    ///
    self.allButton.frame = CGRectMake(0,
                                      0,
                                      buttonWidth,
                                      buttonHeight);
    
    self.outButton.frame = CGRectMake(self.allButton.right,
                                      0,
                                      buttonWidth,
                                      buttonHeight);
    
    self.inButton.frame = CGRectMake(self.outButton.right,
                                     0,
                                     buttonWidth,
                                     buttonHeight);
    
    self.failedButton.frame = CGRectMake(self.inButton.right,
                                         0,
                                         buttonWidth,
                                         buttonHeight);
    
    self.sliderView.frame = CGRectMake(0, self.height-2, 16, 2);
    self.sliderView.centerX = self.allButton.centerX;
}

- (void)setCurrentIndex:(NSInteger)index{
    if(0==index){
        [self onSelectTabAction:self.allButton];
    } else if(1==index){
        [self onSelectTabAction:self.outButton];
    } else if(2==index){
        [self onSelectTabAction:self.inButton];
    }else {
        [self onSelectTabAction:self.failedButton];
    }
}

- (NSInteger)getCurrentIndex{
    if(self.allButton.selected){
        return 0;
    }
    if(self.outButton.selected){
        return 1;
    }
    if(self.inButton.selected){
        return 2;
    }
    return 3;
}

- (void)onSelectTabAction:(UIButton *)sender{
    sender.selected = YES;
    sender.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    
    if([sender isEqual:self.allButton]){
        if(self.actionBlock){
            self.actionBlock(@"", 0);
        }
        self.outButton.titleLabel.font = [UIFont systemFontOfSize:14];
        self.inButton.titleLabel.font = [UIFont systemFontOfSize:14];
        self.failedButton.titleLabel.font = [UIFont systemFontOfSize:14];
        
        self.outButton.selected = NO;
        self.inButton.selected = NO;
        self.failedButton.selected = NO;
        
        [UIView animateWithDuration:0.3 animations:^{
            self.sliderView.centerX = self.allButton.centerX;
        }];
        
    } else if([sender isEqual:self.outButton]){
        if(self.actionBlock){
            self.actionBlock(@"out", 1);
        }
        
        self.allButton.titleLabel.font = [UIFont systemFontOfSize:14];
        self.inButton.titleLabel.font = [UIFont systemFontOfSize:14];
        self.failedButton.titleLabel.font = [UIFont systemFontOfSize:14];
        
        self.allButton.selected = NO;
        self.inButton.selected = NO;
        self.failedButton.selected = NO;
        
        [UIView animateWithDuration:0.3 animations:^{
            self.sliderView.centerX = self.outButton.centerX;
        }];
        
    } else if([sender isEqual:self.inButton]){
        if(self.actionBlock){
            self.actionBlock(@"in", 2);
        }
        
        self.allButton.titleLabel.font = [UIFont systemFontOfSize:14];
        self.outButton.titleLabel.font = [UIFont systemFontOfSize:14];
        self.failedButton.titleLabel.font = [UIFont systemFontOfSize:14];
        
        self.allButton.selected = NO;
        self.outButton.selected = NO;
        self.failedButton.selected = NO;
        
        [UIView animateWithDuration:0.3 animations:^{
            self.sliderView.centerX = self.inButton.centerX;
        }];
        
    } else if([sender isEqual:self.failedButton]){
        if(self.actionBlock){
            self.actionBlock(@"fail", 3);
        }
        
        self.allButton.titleLabel.font = [UIFont systemFontOfSize:14];
        self.outButton.titleLabel.font = [UIFont systemFontOfSize:14];
        self.inButton.titleLabel.font = [UIFont systemFontOfSize:14];
        
        self.allButton.selected = NO;
        self.inButton.selected = NO;
        self.outButton.selected = NO;
        
        [UIView animateWithDuration:0.3 animations:^{
            self.sliderView.centerX = self.failedButton.centerX;
        }];
    }
}

YBViewGetter(sliderView, _sliderView, kThemeColor)

- (UIButton *)allButton{
    if(!_allButton){
        _allButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _allButton.backgroundColor = kClearColor;
        _allButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_allButton setTitle:@"All" forState:UIControlStateNormal];
        [_allButton setTitle:@"All" forState:UIControlStateSelected];
        [_allButton setTitleColor:kThemeHintColor forState:UIControlStateNormal];
        [_allButton setTitleColor:kThemeBlackColor forState:UIControlStateSelected];
        [_allButton addTarget:self action:@selector(onSelectTabAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _allButton;
}

- (UIButton *)outButton{
    if(!_outButton){
        _outButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _outButton.backgroundColor = kClearColor;
        _outButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_outButton setTitle:@"Out" forState:UIControlStateNormal];
        [_outButton setTitle:@"Out" forState:UIControlStateSelected];
        [_outButton setTitleColor:kThemeHintColor forState:UIControlStateNormal];
        [_outButton setTitleColor:kThemeBlackColor forState:UIControlStateSelected];
        [_outButton addTarget:self action:@selector(onSelectTabAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _outButton;
}

- (UIButton *)inButton{
    if(!_inButton){
        _inButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _inButton.backgroundColor = kClearColor;
        _inButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_inButton setTitle:@"In" forState:UIControlStateNormal];
        [_inButton setTitle:@"In" forState:UIControlStateSelected];
        [_inButton setTitleColor:kThemeHintColor forState:UIControlStateNormal];
        [_inButton setTitleColor:kThemeBlackColor forState:UIControlStateSelected];
        [_inButton addTarget:self action:@selector(onSelectTabAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _inButton;
}

- (UIButton *)failedButton{
    if(!_failedButton){
        _failedButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _failedButton.backgroundColor = kClearColor;
        _failedButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_failedButton setTitle:@"Failed" forState:UIControlStateNormal];
        [_failedButton setTitle:@"Failed" forState:UIControlStateSelected];
        [_failedButton setTitleColor:kThemeHintColor forState:UIControlStateNormal];
        [_failedButton setTitleColor:kThemeBlackColor forState:UIControlStateSelected];
        [_failedButton addTarget:self action:@selector(onSelectTabAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _failedButton;
}

@end
